//
//  ButtonView.m
//  礼物说
//
//  Created by dllo on 16/4/23.
//  Copyright © 2016年 sunzhichao. All rights reserved.
//

#import "ButtonView.h"

@interface ButtonView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) BaseCollectionView *collectionView;
@property (nonatomic, retain) BaseView *downView;
@property (nonatomic, retain) BaseLabel *lable;
@property (nonatomic, retain) BaseButton *button;
@property (nonatomic, retain) NSMutableArray *zhongzhuanArr;

@end

@implementation ButtonView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)dealloc {
    /**释放 */
    Block_release(_block);
    [_zhongzhuanArr release];
    [_collectionView release];
    [_button release];
    [_lable release];
    [_downView release];
    [_modelArr release];
    [super dealloc];
}

- (void)setModelArr:(NSMutableArray *)modelArr {
    if (_modelArr != modelArr) {
        [_modelArr release];
        _modelArr = [modelArr retain];
    }
    self.zhongzhuanArr = [NSMutableArray arrayWithArray:_modelArr];
    _collectionView.frame = CGRectMake(0, kWIDTHS(30), kWIDTHS(375), (_zhongzhuanArr.count + 3) / 4 * kWIDTHS(30));
    
    _downView.frame = CGRectMake(0, (_zhongzhuanArr.count + 3) / 4 * kWIDTHS(30) + kWIDTHS(30), kWIDTHS(375), kWIDTHS(667) - 49 - 64 - (_zhongzhuanArr.count + 3) / 4 * kWIDTHS(30) - 30);
}

- (void)createView {
    [super layoutSubviews];
    
    self.lable = [[BaseLabel alloc] initWithFrame:CGRectMake(0, 0, kWIDTHS(335), kHEIGHTS(30))];
    self.lable.backgroundColor = UIColorFromRGB(0XFFFFFF);
    self.lable.text = @"   切换频道";
    self.lable.textColor = [UIColor grayColor];
    [self addSubview:_lable];
    [_lable release];
    
    self.button = [BaseButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(kWIDTHS(335), 0, kWIDTHS(40), kHEIGHTS(30));
//    self.button.backgroundColor = [UIColor whiteColor];
    [self addSubview:_button];
    [_button setImage:[UIImage imageNamed:@"shangsanjiao.png"] forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    
#pragma mark - 创建collectionView
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.itemSize = CGSizeMake(kWIDTHS(93), kHEIGHTS(30));
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, kHEIGHTS(30), kWIDTHS(375), kHEIGHTS(120)) collectionViewLayout:layout];
//    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_collectionView];
    [_collectionView release];
    [layout release];
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [_collectionView registerClass:[ButtonCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    self.downView = [[BaseView alloc] initWithFrame:CGRectMake(0, kHEIGHTS(120) + kWIDTHS(30), kWIDTHS(375), kHEIGHTS(420))];
    [self addSubview:_downView];
    [_downView release];
    _downView.backgroundColor = [UIColor blackColor];
    _downView.alpha = 0.3;
    _downView.userInteractionEnabled = YES;
    
    /**轻拍手势 */
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    [_downView addGestureRecognizer:tap];
    [tap release];
}
/**平拍的方法 */
- (void)tapAction{
    
    self.hidden = YES;
    
}
- (void)buttonAction{
    
    self.hidden = YES;
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _zhongzhuanArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ButtonCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    TitleModel * model = _zhongzhuanArr[indexPath.row];
    cell.title = model.name;
    return cell;
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    _block(indexPath.row);
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
